package com.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author wzm
 * @create 2023-03-16 11:16
 */



//@WebFilter("*.html")
public class LoginFilter implements Filter {

    //过滤器执行的方法
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //过滤器还可以让没有登录的时候不可以访问系统中的其他页面
        //获取登录的session数据
        HttpServletRequest req = (HttpServletRequest) request;
        Object loginUser = req.getSession().getAttribute("loginUser");
        if(loginUser != null){
            chain.doFilter(request,response);
        }else {

            //表示未登录 跳转到登录页
            req.getRequestDispatcher("/login.jsp").forward(request, response);
        }

    }

}
